package com.zk.iotgateway.util;

import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * 日志记录工具类
 * @author:mn.m
 * @create 2019-06-10 上午 11:11
 **/
public class ServerLogUtils {
    /** 服务器正在启动 */
    private static final int STARTING = 0;
    /** 服务器启动失败,已经终止 */
    private static final int START_FAIL = 1;
    /** 服务器启动成功,正在运行 */
    private static final int RUNNING = 2;
    /** 服务器正在停止 */
    private static final int STOPPING = 3;
    /** 服务器已经停止 */
    private static final int STOPPED = 4;
    /* 其他情况*/
    private static final int OTHER = 5;
    /** 服务器状态日志保存地路径 */
    private final String logPath;
    /** 默认的系统状态保存地址,保存地址为:用户当前工作目录/server_status */
    private static final ServerLogUtils instance =
            new ServerLogUtils(System.getProperty("user.dir")
                                       + File.separator + "logs"
                                       + File.separator + "server_status");



    public ServerLogUtils(String path){
        if (StringUtils.isEmpty(path)){
            throw new IllegalArgumentException("log record path must be null");
        }
        this.logPath = path;
        this.checkLogDir();
    }


    /**
     * 取得默认的服务器状态日志
     *
     * @return
     */
    public static ServerLogUtils  getDefaultLog() {
        return instance;
    }

    //写入日志
    private void writeStatusLog(int status) {
        File _logFile = new File(this.logPath);
        FileOutputStream _out = null;
        try {
            _out = new FileOutputStream(_logFile);
            String _time = new SimpleDateFormat("yyyy-MM-dd HH:mm")
                                   .format(new Date());
            String _status = status + "\t" + _time + '\t';
            _out.write(_status.getBytes(Charset.forName("ISO8859-1")));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (_out != null) {
                try {
                    _out.close();
                } catch (IOException e) {
                }
            }
        }
    }

    //写入日志
    private void writeStatusLog(int status,String content) {
        File _logFile = new File(this.logPath);
        FileOutputStream _out = null;
        try {
            _out = new FileOutputStream(_logFile);
            String _time = new SimpleDateFormat("yyyy-MM-dd HH:mm")
                                   .format(new Date());
            String _status = status + "\t" + _time + '\t' + content;
            _out.write(_status.getBytes(Charset.forName("ISO8859-1")));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (_out != null) {
                try {
                    _out.close();
                } catch (IOException e) {
                }
            }
        }
    }


    //写入默认日志
    public static void writeDefaultStatusLog(String content) {
        instance.writeStatusLog(OTHER,content);
    }

    //检查目录是否已创建
    private void checkLogDir() {
        File _logFile = new File(this.logPath).getAbsoluteFile();
        if (_logFile.exists()) {
            return;
        }
        File _parentFile = _logFile.getParentFile();
        if (_parentFile.exists()) {
            return;
        }
        if (_parentFile.mkdirs()) {
            return;
        }
        System.err.println("Can't create the server  log dir[" + _parentFile + ']');
    }


}
